cards_path="image/%i.jpg"

import pygame
import random
from sys import exit
from pygame.locals import *
from utils import kind
pygame.init()

SCREEN_SIZE = (640,480)
CENTER = (320,240)
screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)
card_list = []

x = pygame.image.load(cards_path%1).get_width()
y = pygame.image.load(cards_path%1).get_height()

for i in range(1,55):
	card_list.append(pygame.transform.scale(pygame.image.load(cards_path%i),(x,y)))
random.shuffle(card_list)
rect = Rect(0,0,x,y)
card_rect_list = []
state = []
def kind(n):
	global card_rect_list
	global state
	card_rect_list = []
	state=[]
	if n==1:
		card_rect_list.append(rect.move(CENTER[0]-x/2,CENTER[1]-y/2))
	if n==2:
		card_rect_list.append(rect.move(CENTER[0]-x,CENTER[1]-y/2))
		card_rect_list.append(rect.move(CENTER[0],CENTER[1]-y/2))
	if n==3:
		card_rect_list.append(rect.move(CENTER[0]-x/2,CENTER[1]-y/2))
		card_rect_list.append(rect.move(CENTER[0]-x*3/2,CENTER[1]-y/2))
		card_rect_list.append(rect.move(CENTER[0]+x/2,CENTER[1]-y/2))
	if n==4:		
		card_rect_list.append(rect.move(CENTER[0],CENTER[1]-y))
		card_rect_list.append(rect.move(CENTER[0]-x,CENTER[1]-y))
		card_rect_list.append(rect.move(CENTER[0]-x,CENTER[1]))
		card_rect_list.append(rect.move(CENTER))
	if n==8:		
		card_rect_list.append(rect.move(CENTER[0],CENTER[1]-y))
		card_rect_list.append(rect.move(CENTER[0]+x,CENTER[1]-y))
		card_rect_list.append(rect.move(CENTER[0]+x,CENTER[1]))
		card_rect_list.append(rect.move(CENTER[0]-x,CENTER[1]-y))
		card_rect_list.append(rect.move(CENTER[0]-2*x,CENTER[1]-y))
		card_rect_list.append(rect.move(CENTER[0]-x,CENTER[1]))
		card_rect_list.append(rect.move(CENTER[0]-2*x,CENTER[1]))
		card_rect_list.append(rect.move(CENTER))
	if n==6:
		card_rect_list.append(rect.move(CENTER[0]-x/2,CENTER[1]))
		card_rect_list.append(rect.move(CENTER[0]-x/2,CENTER[1]-y))
		card_rect_list.append(card_rect_list[0].move(-x,0))
		card_rect_list.append(card_rect_list[0].move(x,0))
		card_rect_list.append(card_rect_list[1].move(x,0))
		card_rect_list.append(card_rect_list[1].move(-x,0))
	if n==9:
		for i in range(3):
			for j in range(3):
				card_rect_list.append(rect.move(x*i+170,y*j+16))
	for i in card_rect_list:
		state.append(1)
kind(1)


while True:
	for event in pygame.event.get():
		if event.type == QUIT:
			exit()
		if event.type == KEYDOWN:
			screen.set_clip((0,0),SCREEN_SIZE)
			if event.key == K_4:
				screen.fill((0,0,0))
				kind(4)				
			elif event.key == K_6:
				screen.fill((0,0,0))
				kind(6)
			elif event.key == K_9:
				screen.fill((0,0,0))
				kind(9)
			elif event.key == K_1:
				screen.fill((0,0,0))
				kind(1)
			elif event.key == K_2:
				screen.fill((0,0,0))
				kind(2)
			elif event.key == K_3:
				screen.fill((0,0,0))
				kind(3)
			elif event.key == K_8:
				screen.fill((0,0,0))
				kind(8)
		if event.type == MOUSEBUTTONDOWN:
			if event.button == 3:
				random.shuffle(card_list)
			(_x,_y) = pygame.mouse.get_pos()
			out_rect = True
			for i in range(len(card_rect_list)):
				if card_rect_list[i].collidepoint(_x,_y):
					out_rect = False
					state[i] = 0 if state[i] else 1
			if out_rect :
				for i in range(len(state)):
					state[i]=0 if event.button==1 else 1
	for i in range(len(card_rect_list)):
		screen.set_clip(card_rect_list[i])
		if state[i] == 1:
			screen.blit(card_list[i],screen.get_clip())
		else:
			screen.fill((0,0,0))
			pygame.draw.rect(screen,(255,0,0),card_rect_list[i],3)
		
	pygame.display.update()

	

